<?php

namespace App\Models;

/**
 * App\Models\Page
 *
 * @property int $id
 * @property string $title 标题
 * @property int $group 分组ID:1关于我们2常见问题
 * @property string|null $content 内容
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Page newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Page newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Page query()
 * @method static \Illuminate\Database\Eloquent\Builder|Page whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Page whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Page whereGroup($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Page whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Page whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Page whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Page extends Base
{
    protected $table = 'pages';

    const GROUP_1 = 1;
    const GROUP_2 = 2;
    const GROUP_3 = 3;

    const GROUP_TEXT = [
        self::GROUP_1 => '关于我们',
        self::GROUP_2 => '常见问题',
        self::GROUP_3 => '积分规则',
    ];

    protected $fillable = [
        'id','title','content','group','created_at','updated_at'
    ];


    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('title','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['group']) && $params['group']) {
                    $query ->where('group',$params['group']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate(config('app.limit'));

        $this->stateToText($lists,[
            'group'=> self::GROUP_TEXT,
        ]);
        return $lists;
    }

}
